<?php
namespace app\index\service;

use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowSetModel;
use app\index\model\MenuButModel;
use app\index\model\MenuModel;
use app\index\model\UserGangModel;
use app\index\model\UserRoleModel;
use think\Db;

class MenuService extends CommonService
{

    public function user_menu($pid=0){
        $whereTeshu = "";
        if($this->userinfo['token_roletype'] == 1 && in_array(25, explode(',', $this->userinfo['roleid']))){
            $whereTeshu = "id not in (54,174,56,224)";
        }
        $where[] = ['status','eq',1];
        $where[] = ['ispc','eq',1];
        $whereRole = '';
        if($this->userinfo['issuper'] < 1){
            $userMenuStr = $this->get_user_authmenu();
            $userMenuStr = empty($userMenuStr) ? '0' : $userMenuStr;
            $whereRole = "isrole = 0 or (isrole = 1 and id in (".$userMenuStr."))";
        }

        $data = MenuModel::where($where)->where($whereRole)->where($whereTeshu)->where(['pid'=>$pid])->order('sort ASC')->select()->toArray();
        if($pid > 0){
            foreach ($data as &$item){
                $item['children'] = MenuModel::where($where)->where($whereRole)->where($whereTeshu)->where(['pid'=>$item['id']])->order('sort ASC')->select()->toArray();
            }
        }

        return $data;
    }

    public function user_menu_mob(){
        $where[] = ['status','eq',1];
        $where[] = ['ismob','eq',1];
        $whereRole = '';
        if($this->userinfo['issuper'] < 1){
            $userMenuStr = $this->get_user_authmenu();
            $whereRole = "isrole = 0 or (isrole = 1 and id in (".$userMenuStr."))";
        }

        $data = MenuModel::where($where)->where($whereRole)->where(['pid'=>0])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = MenuModel::where($where)->where($whereRole)->where(['pid'=>$item['id']])->order('sort ASC')->select()->toArray();
            foreach ($item['children'] as &$item2){
                $item2['children'] = MenuModel::where($where)->where($whereRole)->where(['pid'=>$item2['id']])->order('sort ASC')->select()->toArray();
            }
        }

        return $data;
    }

    private function get_user_authmenu(){
        $data = [];
        $role = UserRoleModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'type'=>$this->userinfo['token_roletype']])
            ->where('id','in', $this->userinfo['roleid'])->column('menu');
        foreach ($role as $itemMenu){
            if($itemMenu){
                $data = array_unique(array_merge($data, explode(',', $itemMenu)));
            }
        }
        return implode(',',$data);
    }

    private function get_user_authbut(){
        $data = [];
        $role = UserRoleModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'type'=>$this->userinfo['token_roletype']])
            ->where('id','in', $this->userinfo['roleid'])->column('but');
        foreach ($role as $itemBut){
            $data = array_unique(array_merge($data, explode(',', $itemBut)));
        }
        return $data;
    }


    public function get_butauth($modedir='', $super=0){
        $data = MenuButModel::where(['modedir'=>$modedir,'status'=>1])->order('sort ASC')->select()->toArray();

        if($super < 1){
            $super = $this->userinfo['issuper'];
        }

        $list = [];
        $userButArr = (new MenuService())->get_user_authbut();
        foreach ($data as $v){
            if($super < 1){
                if(!in_array($v['id'], $userButArr)){
                    continue;
                }
            }
            if($v['butname'] == 'add'){
                if($modedir == 'enginlian'){
                    $modedir = 'engin';
                }
                if($modedir == 'cert_use_bid'){
                    $modedir = 'cert_use';
                }
                $set = FlowSetModel::where(['modenum'=>$modedir])->find();
                if($set){
                    $class = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->find();
                    if($class){
                        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'classid'=>$class['id'],'deleted_user'=>0])->find();
                        if($course){
                            $v['butname'] = 'add_flow';
                        }
                    }
                }
            }
            $list[] = $v;
        }

        return $list;
    }

    public function check_flow_start_users($users=''){
//        if(!$users){
//            return true;
//        }
//        if(in_array('user'.$this->userinfo['id'], explode(',', $users))){
//            return true;
//        }
//        if(in_array('dept'.$this->userinfo['deptid'], explode(',', $users))){
//            return true;
//        }
//        return false;
    }


    public function get_miss_where($modedir, $alias=''){

        $str = '';
        $alias = empty($alias) ? '' : $alias.'.';

        if($this->userinfo['issuper'] > 0){
            return '';
        }

        $menuInfo = MenuModel::where(['modedir'=>$modedir])->find();
        if(!$menuInfo && !$menuInfo['id']){
            return $alias."id < 0";
        }

        if($this->userinfo['token_roletype'] == 1){
            //全部权限
        }

        if($this->userinfo['token_roletype'] == 2){
            $enginArr = (new EnginService())->get_user_engin();
            $enginArrStr = implode(',', $enginArr);
            if($enginArrStr){
                $enginStr = '('.$enginArrStr.')';
            }else{
                $enginStr = '(0)';
            }

            if($modedir == 'dealcom'){
                $str = $alias.'id = '.$this->userinfo['dealid'];
            }


            if($modedir == 'engin'){
                $str = $alias.'id in '.$enginStr;
            }
            if($modedir == 'engin_bao'){
                $str = $alias.'id in '.$enginStr;
            }
            if($modedir == 'engin_baoku'){
                $str = $alias.'id in '.$enginStr;
            }
            if($modedir == 'engin_jian'){
                $str = $alias.'id in '.$enginStr;
            }
            if($modedir == 'engin_shi'){
                $str = $alias.'id in '.$enginStr;
            }
            if($modedir == 'engin_yue'){
                $str = $alias.'id in '.$enginStr;
            }
        }

        return $str;
    }


    public function get_opt_where($modedir, $alias=''){
        $str = '';
        $alias = empty($alias) ? '' : $alias.'.';

        if($this->userinfo['issuper'] > 0){
            return '';
        }


        if($this->userinfo['token_roletype'] == 1){
            //全部权限
        }

        if($this->userinfo['token_roletype'] == 2){
            $enginArr = (new EnginService())->get_user_engin();
            $enginArrStr = implode(',', $enginArr);
            if($enginArrStr){
                $enginStr = '('.$enginArrStr.')';
            }else{
                $enginStr = '(0)';
            }

            if($modedir == 'engin'){
                $str = $alias.'id in '.$enginStr;
            }

        }

        return $str;
    }



    public function get_user_authinfo($menuid){
        $data = [];
        $role = UserRoleModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'type'=>$this->userinfo['token_roletype']])
            ->where('id','in', $this->userinfo['roleid'])->column('auth');

        foreach ($role as $itemAuth){
            $auth = json_decode($itemAuth, true);
            if(is_array($auth) && isset($auth[$menuid])){
                $data[] = $auth[$menuid];
            }
        }
        return array_unique($data);
    }

    public function get_dept_subid($id=0, &$idArr=[]){
        if(!$id){
            return [];
        }
        $idArr[] = $id;
        $data = DeptModel::where(['comid'=>$this->userinfo['comid'],'pid'=>$id,'deleted_user'=>0])->column('id');
        foreach ($data as $deptid){
            $this->get_dept_subid($deptid, $idArr);
        }
        return implode(',', $idArr);
    }

    public function get_chan_subid($id=0, $type, &$idArr=[]){
        if(!$id){
            return [];
        }
        if($type == 2){
            $idArr[] = $id;
        }
        $data = DeptModel::field('id,type')->where(['comid'=>$this->userinfo['comid'],'pid'=>$id,'deleted_user'=>0])->select()->toArray();
        foreach ($data as $v){
            $this->get_chan_subid($v['id'], $v['type'], $idArr);
        }
        return implode(',', $idArr);
    }


    public function get_chanid_sons($id){
        $list = [(int)$id];
        $seaarr = [$id];

        while (count($seaarr) > 0) {
            $son = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where('pid','in',$seaarr)->column('id');
            $seaarr = [];
            foreach ($son as $v){
                $list[] = $v;
                $seaarr[] = $v;
            }
        }
        return $list;
    }

}
